<template>
  <div class="area-select">
    <el-col :span="6">
      <el-form-item prop="province">
        <el-select
            v-model="model.province"
            placeholder="请选择省"
            clearable
            style="width: 200px"
        >
          <el-option
              v-for="item in provinceList"
              :key="item.code"
              :label="item.name"
              :value="item.name"
              @click="handleProvinceChange(item.code)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item prop="city">
        <el-select
            v-model="model.city"
            placeholder="请选择市"
            clearable
            style="width: 200px"
            :disabled="!model.province"
        >
          <el-option
              v-for="item in cityList"
              :key="item.code"
              :label="item.name"
              :value="item.name"
              @click="handleCityChange(item.code)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item prop="area">
        <el-select
            v-model="model.area"
            placeholder="请选择区/县"
            clearable
            style="width: 200px"
            :disabled="!model.city"
        >
          <el-option
              v-for="item in areaList"
              :key="item.code"
              :label="item.name"
              :value="item.name"
              @click="handleAreaChange(item.code)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item prop="country">
        <el-select
            v-model="model.country"
            placeholder="请选择乡镇/街道"
            clearable
            style="width: 200px"
            :disabled="!model.area"
        >
          <el-option
              v-for="item in streetList"
              :key="item.code"
              :label="item.name"
              :value="item.name"
              @click="handleCountryChange(item.code)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
  </div>
</template>
<script setup>
import {onMounted} from 'vue'
import { areaSelect } from "@/composables/AreaSelect"
// 定义组件属性
const props = defineProps({
  model: {
    type: Object,
    required: true
  }
})
// 定义事件
const emit = defineEmits(['update:model'])
// 使⽤ areaSelect 组合式函数
const {
  provinceList,
  cityList,
  areaList,
  streetList,
  initProvinceList,
  provinceChange,
  cityChange,
  areaChange
} = areaSelect()
// 省份选择改变时
function handleProvinceChange(value) {
  // 重置市、区、街道
  props.model.city = ''
  props.model.area = ''
  props.model.country = ''
  cityList.value = []
  areaList.value = []
  streetList.value = []
  if (value) {
    provinceChange(value)
  }
  emit('update:model', props.model)
}
// 市选择改变时
function handleCityChange(value) {
  // 重置区、街道
  props.model.area = ''
  props.model.country = ''
  areaList.value = []
  streetList.value = []
  if (value) {
    cityChange(value)
  }
  emit('update:model', props.model)
}
// 区/县选择改变时
function handleAreaChange(value) {
  // 重置街道
  props.model.country = ''
  streetList.value = []
  if (value) {
    areaChange(value)
  }
  emit('update:model', props.model)
}
// 街道选择改变时
function handleCountryChange(value) {
  // 可以在这⾥添加街道选择后的处理逻辑
  emit('update:model', props.model)
}
// 初始化省份列表
onMounted(() => {
  initProvinceList()
})
</script>
<style scoped>
.area-select {
  display: flex;
  flex-wrap: wrap;
}
</style>